/**
 * 
 */
package com.whf15.o2o.web.local;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whf15.o2o.dto.LocalAuthExecution;
import com.whf15.o2o.entity.LocalAuth;
import com.whf15.o2o.entity.PersonInfo;
import com.whf15.o2o.enums.LocalAuthStateEnum;
import com.whf15.o2o.exceptions.LocalAuthOperationException;
import com.whf15.o2o.service.LocalAuthService;
import com.whf15.o2o.util.CodeUtil;
import com.whf15.o2o.util.HttpServletRequestUtil;

/**   
* 项目名称：o2o   
* 类名称：LocalAuthController   
* 类描述：   
* @version 
* @author whfo 
* @date 2021年9月3日 下午5:19:26        
*/
@Controller
@RequestMapping(value = "local",method = {RequestMethod.GET,RequestMethod.POST})
public class LocalAuthController {
	@Autowired
	private LocalAuthService localAuthService;
	
	/**
	* 方法描述：   将用户信息与平台账号绑定
	* @author whfo 
	* @date 2021年9月3日 下午8:23:25 
	* @param request
	* @return
	*/
	@RequestMapping(value="/bindlocalauth",method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> bindLocalAuth(HttpServletRequest request){
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		//验证码校验
		if(!CodeUtil.checkVerifyCodeActual(request)) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "输入了错误的验证码");
			return modelMap;
		}
		//获取输入的账号
		String userName = HttpServletRequestUtil.getString(request, "userName");
		//获取输入的密码
		String password = HttpServletRequestUtil.getString(request, "password");
		//从session中获取账号密码以及当前的用户session非空
		PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");
		//非空判断，要求账号密码以及当前的用户session非空
		if(userName != null && password != null && user != null && user.getUserId() != null) {
			//创建LocalAuth对象并赋值
			LocalAuth localAuth = new LocalAuth();
			localAuth.setUsername(userName);
			localAuth.setPassword(password);
			localAuth.setPersonInfo(user);
			//绑定账号
			LocalAuthExecution lae = localAuthService.bingLocalAuth(localAuth);
			if(lae.getState() == LocalAuthStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("errMsg", lae.getStateInfo());
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "用户名和密码均不为空");
		}
		return modelMap;
		
	}
	/**
	* 方法描述：   修改密码
	* @author whfo 
	* @date 2021年9月3日 下午8:24:16 
	* @param request
	* @return
	*/
	@RequestMapping(value="/changelocalpwd",method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> changeLocalPwd(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		//验证码校验
		if(!CodeUtil.checkVerifyCodeActual(request)) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "输入了错误的验证码");
			return modelMap;
		}
		//获取输入的账号
		String userName = HttpServletRequestUtil.getString(request, "userName");
		//获取输入的密码
		String password = HttpServletRequestUtil.getString(request, "password");
		//获取新密码
		String newPassword = HttpServletRequestUtil.getString(request, "newPassword");
		PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");
		//非空判断，要求账号密码以及当前的用户session非空
		if(userName != null && password != null && user != null && user.getUserId() != null
				&& !password.equals(newPassword)) {
			try {
				//查看原先账号，看看与输入的账号是否一直，不一致则认为时非法操作
				LocalAuth localAuth = localAuthService.getLocalAuthByUserId(user.getUserId());
				if(localAuth == null || !localAuth.getUsername().equals(userName)) {
					//不一致则直接退出
					modelMap.put("success", false);
					modelMap.put("errMsg", "输入的账号非本次登录的账号");
					return modelMap;
				}
				//修改平台账号的用户密码
				LocalAuthExecution lae = localAuthService.modifyLocalAuth(user.getUserId(), userName, password, newPassword);
				if(lae.getState() == LocalAuthStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("errMsg", lae.getStateInfo());
				}
			} catch (LocalAuthOperationException e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
				return modelMap;
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "你输入左密码没啊？");
		}
		return modelMap;		
	}
	/**
	* 方法描述：   
	* @author whfo 
	* @date 2021年9月3日 下午8:24:26 
	* @param request
	* @return
	*/
	@RequestMapping(value="/logincheck",method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> logincheck(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		//获取是否需要进行验证码校验的标识符
		boolean needVerify = HttpServletRequestUtil.getBoolean(request, "needVerify");
	
		//验证码校验
		if(needVerify&&!CodeUtil.checkVerifyCodeActual(request)) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "输入了错误的验证码");
			return modelMap;
		}
		//获取输入的账号
		String userName = HttpServletRequestUtil.getString(request, "userName");
		//获取输入的密码
		String password = HttpServletRequestUtil.getString(request, "password");
		
		//非空校验
		if(userName != null && password != null) {
			//传入账号和密码去获取平台账号信息
			LocalAuth localAuth = localAuthService.getLocalAuthByUsernameAndPwd(userName, password);
			if(localAuth != null) {
				//若能获取到账号信息则登录成功
				modelMap.put("success", true);
				//同时在session里面设置用户信息
				request.getSession().setAttribute("user", localAuth.getPersonInfo());
			}else {
				modelMap.put("success", false);
				modelMap.put("errMsg", "用户名或密码错误");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "用户名和密码均不为空");
		}
		return modelMap;
	}
	
	
	/**
	* 方法描述：   当用户点击等处按钮时注销session
	* @author whfo 
	* @date 2021年9月3日 下午8:20:47 
	* @param request
	* @return
	*/
	@RequestMapping(value="/logout",method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> logout(HttpServletRequest request){
		Map<String, Object>modelMap = new HashMap<String,Object>();
		
		//将用户session置为空
		request.getSession().setAttribute("user", null);
		modelMap.put("success", true);
		return modelMap;
	}
	
}

